<?php
include_once(_CORE_PATH. "mapper_prototype.php");
class ImageMapper extends mapper_prototype
{
	private $_uploadError;
	
	public function __construct()
	{
		parent::__construct('image');
	}
	
	static public function GetInstance()
	{
		return new self();		
	}	
	
    public function GetImageThumbFolder($thumbFolder, $realFolder = false)
    {
        if (!$this->IsThumbFolder($thumbFolder)) throw new Exception('Директория ' .$thumbFolder. ' не найдена');
        
        $realFolder = trim($realFolder, '/');
        if (strlen($realFolder)) $realFolder .= '/';
        return MozgCfg::GetProp('global/image//imageThumbsFolder').$thumbFolder. '/' .$realFolder;
    }
    
    public function IsThumbFolder($thumbFolder)
    {
        return !is_null(MozgCfg::GetProp('global/image//thumbSizes/' .$thumbFolder));        
    }
    
    public function GetThumbFolderSizes($thumbFolder)
    {
        if (!$this->IsThumbFolder($thumbFolder)) throw new Exception('Директория ' .$thumbFolder. ' не найдена');
        return MozgCfg::GetProp('global/image//thumbSizes/' .$thumbFolder);                
    }
    
    public function UploadResize($dstPath, $newFileName, $uploadedFilePath, $maxSize)
    {
		$imageFile = $this->Libs()->image->GetImage($uploadedFilePath);
		
		$this->SetUploadError();
		
		if (!$imageFile->IsImage()) 
		{    
		    $this->SetUploadError($imageFile->GetError());
		    return false;
		}

		$newFullFileName = $imageFile->Resize($dstPath, $newFileName, $maxSize); 
		
		if ($imageFile->IsError()) 
		{    
		    $this->SetUploadError($imageFile->GetError());               
		    return false;
		} 

		return $newFullFileName; 
    }
    
    private function SetUploadError($message = '')
    {
        $this->_uploadError = $message;         
    }

    public function GetUploadError()
    {
        return $this->_uploadError;         
    }    

    private function GetUploadErrorMessage($errorCode)
    {
        switch ($errorCode)
        {
            case UPLOAD_ERR_FORM_SIZE:
            case UPLOAD_ERR_INI_SIZE:
	            $m = 'Превышен размер загружаемого файла';    
	            break;
            
            case UPLOAD_ERR_PARTIAL:
                $m = 'Загружаемый файл был получен только частично';
                break;
            
            case UPLOAD_ERR_NO_FILE:
                $m = 'Файл не был загружен';
                break;
            
            default: $m = 'Неизвестная ошибка';
                                    
        }
        
        return $m;
    }    
    
}